package demo8;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-12-01
 * Time: 15:52
 */
class Counter {
    Queue<Integer> queue = new LinkedList<>();
}
public class Main {
    static int m;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        m = scanner.nextInt();
        int[] num = new int[n];
        for (int i = 0; i < n; i++) {
            num[i] = scanner.nextInt();
        }
        heapSort(num, Math.min(m, n));
        dfs(num, m);
    }

    private static void dfs(int[] num, int n) {
        if(n == 1) {
            System.out.print(num[0]);
        } else {
            int t = num[0];
            num[0] = Integer.MAX_VALUE;
            shiftDown(num, 0, m);
            dfs(num, n - 1);
            System.out.print(" " + t);
        }
    }

    private static void heapSort(int[] num, int m) {
        createHeap(num, m);
        for (int i = m; i < num.length; i++) {
            if(num[i] > num[0]) {
                num[0] = num[i];
                shiftDown(num, 0, m);
            }
        }
    }
    private static void createHeap(int[] num, int m) {
        for (int p = (m - 1 - 1) / 2; p >= 0; p--) {
            shiftDown(num, p, m);
        }
    }

    private static void shiftDown(int[] num, int parent, int end) {
        int child = 2 * parent + 1;
        while (child < end) {
            if(child + 1 < end && num[child] > num[child + 1]) {
                child++;
            }
            if(num[parent] > num[child]) {
                int tmp = num[parent];
                num[parent] = num[child];
                num[child] = tmp;
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }

    private static void radixSort(int[] num) {
        int length = getMaxLength(num);
        for(int i = 1; i <= length; i++) {
            sort(num, i);
        }
    }

    private static void sort(int[] num, int i) {
        Counter[] counter = new Counter[10];
        for (int j = 0; j < 10; j++) {
            counter[j] = new Counter();
        }
        for (int k : num) {
            int r = get(k, i);
            counter[r].queue.add(k);
        }
        int k = 0;
        for (int j = 0; j < 10; j++) {
            while (counter[j].queue.size() != 0) {
                num[k++] = counter[j].queue.poll();
            }
        }
    }

    private static int get(int num, int i) {
        int ans = 0;
        while (i != 0) {
            ans = num % 10;
            num /= 10;
            i--;
        }
        return ans;
    }

    private static int getMaxLength(int[] num) {
        int maxNum = getMaxNum(num);
        int count = 0;
        while (maxNum != 0) {//123//12//1//0
            count++;//1//2//3
            maxNum /= 10;//12//1//0
        }
        return count;
    }

    private static int getMaxNum(int[] num) {
        int max = num[0];
        for (int i = 1; i < num.length; i++) {
            max = Math.max(max, num[i]);
        }
        return max;
    }
}
